package com.jt.aop;

import com.jt.service.DeptServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class SpringAOP {
    @Pointcut("within(com.jt.service.DeptServiceImpl)")
    public void point(){

    }
    @Before("point()")
    public void before(JoinPoint joinPoint){
        System.out.println("before");
        System.out.println("获取目标对象的类型"+joinPoint.getTarget().getClass());
        System.out.println("获取目标对象的类名"+joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("获取目标对象的方法名"+joinPoint.getSignature().getName());
        System.out.println("获取方法参数"+ Arrays.toString(joinPoint.getArgs()));
        ;
    }
   @AfterReturning(value = "point()",returning =  "result")
    public void afterreturn(JoinPoint joinPoint,Object result){
        System.out.println("afterreturn:"+result);
    }
//   @AfterThrowing(value = "point()",throwing = "e")
    public void afterthowing(Exception e){
        System.out.println(e.getMessage());
       System.out.println(e.getClass());
    }
   @After("point()")
    public void after(){
        System.out.println("after");
    }
    @Around("point()")
    public Object doaround(ProceedingJoinPoint joinPoint){
        System.out.println("代理环绕开始");
        Object proceed=null;
        try {
            Long start=System.currentTimeMillis();
             proceed = joinPoint.proceed();
            Long end=System.currentTimeMillis();
            System.out.println("消耗时间"+(start-end)+"ms");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }finally {
            System.out.println("代理环绕结束");
        }
        return proceed;
    }
//    @Around("point()")
//    public void around(){
//        System.out.println("around");
//    }

}
